const {
	Controller
} = require('egg');
const captchapng = require('captchapng'); //base64图形验证码

class HomeController extends Controller {
	constructor(ctx, app) {
		super(ctx, app); // 如果需要在构造函数做一些处理，一定要有这句话，才能保证后面 `this.ctx`的使用。
		// 就可以直接通过 this.ctx 获取 ctx 了
		// 还可以直接通过 this.app 获取 app 了
	}

	resultsuccess(data, message) {
		return this.ctx.body = {
			success: true,
			code: 200,
			message: message || '操作成功',
			data: data
		};
	}
	resultfail(message, code) {
		return this.ctx.body = {
			success: false,
			code: code || 300,
			message: message || '操作失败',
		};
	}
	async index() {
		const {
			ctx
		} = this;
		ctx.body = 'hi, egg';
	}
	async adduser() {
		const {
			ctx
		} = this;
		try {
			const data = ctx.request.body;
			const userList = await ctx.service.user.adduser(data);
			ctx.body = {
				success: true,
				data: userList
			};
		} catch (error) {
			ctx.body = {
				success: false,
				error
			};
		}
	}
	//base64图形验证码
	async captcha() {
		try {
			let captchaVal = parseInt(Math.random() * 9000 + 1000)
			var p = new captchapng(80, 30, captchaVal); // width,height,numeric captcha
			p.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
			p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
			var img = p.getBase64();
			this.ctx.session.code = captchaVal;
			var data = {
				uuid: captchaVal,
				img: "data:image/png;base64," + img
			}
			this.resultsuccess(data)
		} catch (error) {
			this.ctx.body = {
				success: false,
				error
			};
		}
	}

	async login() { //登录
		try {
			if (!this.ctx.request.body.phone) return this.resultfail('账号不能为空');
			if (!this.ctx.request.body.password) return this.resultfail('密码不能为空');
			// if (!this.ctx.request.body.code) return this.resultfail('验证码不能为空');
			// if (this.ctx.request.body.uuid != this.ctx.request.body.code) return this.resultfail('验证码错误');
			const user = await this.ctx.service.user.find(this.ctx.request.body.phone);
			if (!user) return this.resultfail('账号错误');
			if (this.ctx.request.body.password != user.password) return this.resultfail('密码错误');
			const phone = this.ctx.request.body.phone;
			const token = this.app.jwt.sign({
				phone
			}, this.app.config.security.jwt.key)
			this.resultsuccess(token)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async dashboard() { //首页汇总
		try {
			const data = await this.ctx.service.home.getTotal();
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async dashboardchart() { //首页报表
		try {
			const data = await this.ctx.service.home.getChart();
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async userlist() { //会员列表
		try {
			const data = await this.ctx.service.home.getuser(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async approveAgency() { //更改为代理
		try {
			const data = await this.ctx.service.home.approveAgency(this.ctx.request.body);
			if (data) {
				this.resultsuccess(data)
			} else {
				this.resultfail('修改失败')
			}

		} catch (error) {
			this.resultfail(error)
		}
	}

	async addresslist() { //地址列表
		try {
			const data = await this.ctx.service.home.getaddress(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async collectlist() { //收藏列表
		try {
			const data = await this.ctx.service.home.collectlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async footprintlist() { //足迹列表
		try {
			const data = await this.ctx.service.home.footprint(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async searchhistoryt() { //搜索历史
		try {
			const data = await this.ctx.service.home.searchhistoryt(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	// async feedbacklist() { //测试延时操作
	// 	try {
	// 		const data = await this.ctx.service.home.delay(3000);
	// 		this.resultsuccess(data)
	// 	} catch (error) {
	// 		this.resultfail(error)
	// 	}
	// }

	async feedbacklist() { //意见反馈
		try {
			const data = await this.ctx.service.home.feedbacklist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async regionlist() { //行政区数据
		try {
			const data = await this.ctx.service.home.regionlist();
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async brandlist() { //品牌数据
		try {
			const data = await this.ctx.service.home.brandlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}


	async categorylist() { //商品分类
		try {
			const data = await this.ctx.service.home.categorylist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async orderlist() { //订单列表
		try {
			// const data = await this.ctx.service.home.orderlist(this.ctx.query);
			var parameter = this.ctx.query
			const query = {
				limit: parameter.limit,
				offset: (parameter.page - 1) * parameter.limit,
			};

			const whereobe = {
				user_id: parameter.userId,
				order_sn: parameter.orderSn
			}
			if (!whereobe.user_id) {
				delete whereobe.user_id;
			}
			if (!whereobe.order_sn) {
				delete whereobe.order_sn;
			}
			// const user = await this.ctx.model.User.findAll(query);
			let DtsOrder = this.ctx.model.DtsOrder;
			let DtsOrderGoods = this.ctx.model.DtsOrderGoods;
			let User = this.ctx.model.User;
			//hasMany一对多
			DtsOrder.hasMany(DtsOrderGoods, {
				foreignKey: "order_id", //副表
				sourceKey: "id", //主表
			})
			DtsOrder.belongsTo(User, {
				foreignKey: "user_id", //主表
				targetKey: 'id' //副表
			})


			// console.log('==========',whereobe)
			let result = await DtsOrder.findAll({
				include: [{
					model: DtsOrderGoods
				}, {
					model: User
				}],
				where: whereobe,
				limit: Number(parameter.limit),
				offset: Number((parameter.page - 1) * parameter.limit)
			})

			const Total = await this.app.mysql.query(
				`select COUNT(*) AS total_orders from dts_order where ${parameter.userId?'user_id = '+parameter.userId:"user_id != ''" } AND ${parameter.orderSn?'order_sn = '+parameter.orderSn:"order_sn != ''" };`
				);
			var data = {
				"items": result,
				"total": Total[0].total_orders
			};
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async orderdetail() { //订单详情
		try {
			const data = await this.ctx.service.home.orderdetail(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async createissue() { //新增通用问题
		try {
			const data = await this.ctx.service.home.createissue(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async issuelist() { //通用问题
		try {
			const data = await this.ctx.service.home.issuelist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async updateissue() { //编辑通用问题
		try {
			const data = await this.ctx.service.home.updateissue(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async deleteissue() { //删除通用问题
		try {
			const data = await this.ctx.service.home.deleteissue(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async keywordlist() { //关键词
		try {
			const data = await this.ctx.service.home.keywordlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async goodslist() { //商品列表
		try {
			const data = await this.ctx.service.home.goodslist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	async catAndBrand() { //分类及品牌
		try {
			const data = await this.ctx.service.home.catAndBrand(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	async creategoods() { //添加商品
		try {
			const data = await this.ctx.service.home.creategoods(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	async commentlist() { //评论列表
		try {
			const data = await this.ctx.service.home.commentlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async commentdelete() { //删除评论列表
		try {
			const data = await this.ctx.service.home.commentdelete(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async articlelist() { //公告列表
		try {
			const data = await this.ctx.service.home.articlelist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async articledetail() { //公告详情
		try {
			const data = await this.ctx.service.home.articledetail(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async adlist() { //广告管理
		try {
			const data = await this.ctx.service.home.adlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async createAd() { //添加广告管理
		try {
			const data = await this.ctx.service.home.createAd(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async deleteAd() { //删除广告管理
		try {
			const data = await this.ctx.service.home.deleteAd(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async updateAd() { //编辑广告管理
		try {
			const data = await this.ctx.service.home.updateAd(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async couponlist() { //优惠券管理
		try {
			const data = await this.ctx.service.home.couponlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async couponread() { //优惠券详情
		try {
			const data = await this.ctx.service.home.couponread(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async couponlistuser() { //优惠券用户拥有量
		try {
			const data = await this.ctx.service.home.couponlistuser(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async topiclist() { //优惠券用户拥有量
		try {
			const data = await this.ctx.service.home.topiclist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async grouponlistRecord() { //团购活动列表
		try {
			const data = await this.ctx.service.home.grouponlistRecord(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async grouponlist() { //团购规则列表
		try {
			const data = await this.ctx.service.home.grouponlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async creategroupon() { //新增团购规则列表
		try {
			const data = await this.ctx.service.home.creategroupon(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async menulist() { //菜单列表
		try {
			const data = await this.ctx.service.home.menulist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async createmenu() { //新增菜单列表
		try {
			const data = await this.ctx.service.home.createmenu(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async updatemenu() { //编辑菜单列表
		try {
			const data = await this.ctx.service.home.updatemenu(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async deletemenu() { //删除菜单列表
		try {
			var parter = this.ctx.request.body;
			if (parter.children.length > 0) {
				return this.resultfail('请先删除下级菜单')
			}
			delete parter.children;
			const data = await this.ctx.service.home.deletemenu(parter);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async adminlist() { //管理员列表
		try {
			const data = await this.ctx.service.home.adminlist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async createadmin() { //添加管理员列表
		try {
			const data = await this.ctx.service.home.createadmin(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}

	async updateadmin() { //编辑管理员列表
		try {
			const data = await this.ctx.service.home.updateadmin(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async deleteadmin() { //删除管理员
		try {
			const data = await this.app.mysql.delete(`dts_admin`, {id: this.ctx.request.body.id});
			this.resultsuccess('删除成功')
		} catch (error) {
			this.resultfail(error)
		}
	}
	async rolelist() { //角色列表
		try {
			const data = await this.ctx.service.home.rolelist(this.ctx.query);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async roledelete() { //删除角色列表
		try {
			const data = await this.ctx.service.home.roledelete(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	async rolecreate() { //添加角色列表
		try {
			const data = await this.ctx.service.home.rolecreate(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	async roleupdate() { //编辑角色列表
		try {
			const data = await this.ctx.service.home.roleupdate(this.ctx.request.body);
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	async roleoptions() { //管理员列表
		try {
			var list = await this.ctx.model.Role.findAll({
				order: [
					['add_time', 'desc']
				]
			})
			this.resultsuccess(list)
		} catch (error) {
			this.resultfail(error)
		}
	}
	
	
	async datalist() { //数据列表
		try {
			const data = await this.ctx.service.home.datalist(this.ctx.query);
			console.log('22222222222', data)
			this.resultsuccess(data)
		} catch (error) {
			this.resultfail(error)
		}
	}
}

module.exports = HomeController;